package com.aji.food1.service.impl;

import com.aji.food1.domain.Food1;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aji.food1.domain.Sep;
import com.aji.food1.service.SepService;
import com.aji.food1.mapper.SepMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

/**
* @author 82527
* @description 针对表【sep(光谱表)】的数据库操作Service实现
* @createDate 2025-11-11 12:25:00
*/
@Service
public class SepServiceImpl extends ServiceImpl<SepMapper, Sep>
    implements SepService{
    @Autowired
    private SepMapper sepMapper;

    @Override
    @Async
    public void saveByUrl(Food1 entity) {
        //根据url下载txt文件
        System.out.println("id为");
        System.out.println(entity.getId());
        String urlStr = entity.getDpt(); // 替换为实际的URL
        String filePath = "temp.txt";

        try {
            URL url = new URL(urlStr);
            URLConnection conn = url.openConnection();
            try (InputStream inputStream = conn.getInputStream();
                 // 使用FileOutputStream时，默认会覆盖已有文件（确保新建）
                 FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {

                int bytesum = 0;
                int byteread;
                byte[] buffer = new byte[1024];
                while ((byteread = inputStream.read(buffer)) != -1) {
                    bytesum += byteread;
                    System.out.println("已读取字节数: " + bytesum);
                    fileOutputStream.write(buffer, 0, byteread);
                }
                System.out.println("文件下载完成，总大小: " + bytesum + " 字节");
            }

            // 读取并输出文件内容
            System.out.println("\n文件内容如下:");
            try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] split = line.split(" ");
                    for(int i = 0;i<split.length;i++){
                        System.out.println(split[i]);
                        String[] s = split[i].split("\t");
                        //处理数据
                        Sep sep = new Sep();
                        sep.setFood1Id(entity.getId());
                        sep.setStrength(Double.parseDouble(s[1]));
                        sep.setWaveLength(Integer.parseInt(s[0]));
                        sepMapper.insert(sep);
                    }

                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeByFood1Ids(List<Integer> ids) {
        LambdaQueryWrapper<Sep> compoundLambdaQueryWrapper  =new LambdaQueryWrapper<Sep>().select()
                        .in(ids!=null,Sep::getFood1Id,ids);
        sepMapper.delete(compoundLambdaQueryWrapper);
    }
}




